﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using intelliSys.UI.Interaction.MouseGesture;
using System.Reflection;

namespace MouseGestureDemo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
            this.MouseLeftButtonDown += new MouseButtonEventHandler(MainWindow_MouseLeftButtonDown);
        }

        void MainWindow_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
        Shake shk;
        Hold hld;
        Glide gld;
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            shk = new Shake(this);
            shk.OnSensed += new EventHandler<MouseGestureEventArgBase>(shk_OnSensed);
            hld = new Hold(this);
            hld.OnSensed += new EventHandler<MouseGestureEventArgBase>(hld_OnSensed);
            gld = new Glide(this);
            gld.OnSensed += new EventHandler<MouseGestureEventArgBase>(gld_OnSensed);
            gld.InstantFire = false;
            gld.Intensity = 20;
            gld.Speed = 10;
            gld.Distance = 100;
            foreach (PropertyInfo pi in typeof(intelliSys.UI.Interaction.MouseGesture.MouseGestureBase).GetProperties())
            {
                Group.Header = "intelliSys.UI.Interaction.MouseGesture.MouseGestureBase";
                StackPanel stknpl = new StackPanel() { Orientation = Orientation.Horizontal };
                stknpl.Children.Add(new TextBlock() { Text = pi.Name, Foreground = new SolidColorBrush(Colors.Gray) });
                stackPnl.Children.Add(stknpl);
            }
            // System.Threading.Thread.CurrentThread.Name = "Main";
        }

        void tb_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                textBlock1.Text = "==========CHANGE PARAMETER============";
                AssociatedTextBox tb = sender as AssociatedTextBox;
                var methods = tb.associatedObj.GetType().GetProperty(tb.propertyinfo.Name).PropertyType.GetMethods().Where<MethodInfo>(o =>
                    {
                       return o.Name == "Parse" && o.GetParameters().Count() == 1;
                    });
                var obj = methods.First().Invoke(tb.associatedObj.GetType().GetProperty(tb.propertyinfo.Name).PropertyType, new object[]{ tb.Text });
                tb.associatedObj.GetType().GetProperty(tb.propertyinfo.Name).SetValue(tb.associatedObj, obj, null);
            }
            catch (Exception ex)
            {
                try
                {
                    textBlock1.Text = "========REFLECTION FAILED=========\n" + ex.InnerException.Message;
                }
                catch
                {
                    textBlock1.Text = ex.Message;
                }
            }
        }

        public void Reflect(object obj)
        {
            stackPnl.Children.Clear();
            foreach (PropertyInfo pi in obj.GetType().GetProperties())
            {
                Group.Header = obj.GetType().FullName;
                StackPanel stknpl = new StackPanel() { Orientation = Orientation.Horizontal };
                stknpl.Children.Add(new TextBlock() { Text = pi.Name });
                if (pi.CanWrite)
                {
                    AssociatedTextBox tb = new AssociatedTextBox() { Text = pi.GetValue(obj, null).ToString(), propertyinfo = pi, associatedObj = obj };
                    tb.TextChanged += new TextChangedEventHandler(tb_TextChanged);
                    stknpl.Children.Add(tb);
                }
                else
                {
                    stknpl.Children.Add(new TextBlock() { Text = pi.GetValue(obj, null).ToString(), Foreground = new SolidColorBrush(Colors.Gray) });
                }
                stackPnl.Children.Add(stknpl);
            }
        }
        void gld_OnSensed(object sender, MouseGestureEventArgBase e)
        {
            status.Text = "Glide Detected";
            Reflect(gld);
        }

        void hld_OnSensed(object sender, MouseGestureEventArgBase e)
        {
            status.Text = "Hold Detected";
            Reflect(hld);
        }

        void shk_OnSensed(object sender, MouseGestureEventArgBase e)
        {
            status.Text = "Shake Detected";
            Reflect(shk);
        }

        private void instant_Click(object sender, RoutedEventArgs e)
        {
        }
    }
    class AssociatedTextBox : TextBox
    {
        public PropertyInfo propertyinfo;
        public object associatedObj;
    }
}
